/* Copyright (c) 2008 LepomisLab.com, Andrey Turkin
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "NSTableColumn.h"
#include "NSTableView.h"
#include "NSKeyedUnarchiver.h"

IMPLEMENT_DYNAMIC_CLASS(NSTableColumn,NSObject)

NSTableColumn::NSTableColumn() : NSObject(), _resizeable(true), _editable(false), m_table(NULL)
{
	_value = new NSArray();
	exposeBinding(wxT("value"));
}

void NSTableColumn::initWithUnarchiver(NSKeyedUnarchiver *archiver)
{
	if (archiver->contains(wxT("NSIdentifier")))
		archiver->decodeStringForKey(wxT("NSIdentifier"));
	archiver->decodeObjectForKey(wxT("NSDataCell"));
	archiver->decodeObjectForKey(wxT("NSHeaderCell"));
	if (archiver->contains(wxT("NSIsResizeable")))
		_resizeable = archiver->decodeBoolForKey(wxT("NSIsResizeable"));
	if (archiver->contains(wxT("NSIsEditable")))
		_editable = archiver->decodeBoolForKey(wxT("NSIsEditable"));
	if (archiver->contains(wxT("NSWidth")))
		_width = archiver->decodeFloatForKey(wxT("NSWidth"));
	if (archiver->contains(wxT("NSMaxWidth")))
		_maxWidth = archiver->decodeFloatForKey(wxT("NSMaxWidth"));
	if (archiver->contains(wxT("NSMinWidth")))
		_minWidth = archiver->decodeFloatForKey(wxT("NSMinWidth"));
}

void NSTableColumn::_setValue(const wxString& key, wxObject* value)
{
	if (key == wxT("value"))
	{
		if (value)
		{
			_value = wxDynamicCast(value, NSMutableArray);
			wxASSERT(_value); 
		}
	} else
		NSObject::_setValue(key, value);
}

wxObject* NSTableColumn::_getValue(const wxString& key)
{
	if (key == wxT("value"))
	{
		return _value;
	} else
		return NSObject::_getValue(key);
}

void NSTableColumn::bind(const wxString& bind, NSObject* target, const wxString& keypath, NSObject* options)
{
	NSObject::bind(bind, target, keypath, options);

	if (bind == wxT("value")) /* notify view of target */
		if (m_table)
			m_table->_column_bound(target);
}
